#!/bin/bash

path=/boot/config/plugins/dynamix.file.integrity
list=/tmp/$$_list
hash=${1//,/ }
hname=$2

update() {
  time=$SECONDS
  new=$((count*100/files))
  if [[ $new -gt $old ]]; then
    echo -ne "<span class='array'>Processing $disk entries... $new%</span>\r"
    old=$new
  fi
}
function cleanup() {
  rm -f $list.*
  rm -rf /disk*.export.$hname.corz
  exit 0
}

trap cleanup EXIT

for disk in $hash; do
  export=$path/export/$disk.export.$hname.hash
  if [[ -f $export ]]; then
    out=1
    echo -ne "<span class='warn'>Process hash file: $(basename $export)\n</span>"
    sed -r "s:^(\S+) \*/mnt/$disk/:\1|:" $export|sort -k2 -o$list.0
    count=0; old=0; files=$(wc -l $list.0|cut -d' ' -f1); time=0;
    ifs=IFS; IFS='|'
    while read -ra line; do
      folder=$(dirname "${line[1]}")
      file=$(basename "${line[1]}")
      mkdir -p "/$disk.export.$hname.corz/$folder"
      echo -e "#$hname#$file#$(stat -c %y "/mnt/$disk/${line[1]}" 2>/dev/null|cut -d. -f1)\n${line[0]}  $file" >>"/$disk.export.$hname.corz/$folder/$(basename "$folder").hash"
      ((count++))
      [[ $SECONDS -gt $time ]] && update
    done <$list.0
    IFS=$ifs
    zip -qmr $path/export/$disk.export.$hname.corz.zip /$disk.export.$hname.corz
    echo -ne "<span class='system'>Hash export to Corz conversion of $disk completed\n</span>"
  else
    out=0
    echo -ne "<span class='login'>File $disk.export.$hname.hash not found\n</span>"
  fi
done
[[ $out -eq 1 ]] && echo -e "\n<span class='array'>See folder <b>hash files</b> for the generated output</span>"
